﻿(function($){
	var window=this;
	$.selectDate=function(){};
	$.selectDate.defaults={
		IsYear:true,
		divClass:'year_show',
		itemLength:60,
		layerClass:'year_control',
		showHeight:418,
		height:418,
		yInputId:"birth_date_y",
		mInputId:"birth_date_m",
		mdivClass:"month_show",
		soFar:false,
		linkage: true,
		left:0,
		languageId:1,
		close:null,
		closeM:null
	};
	
	$.fn.extend({
		DateLink:function(options){
			var thisObj = this;
			var inputR=$(this);
			var options=$.extend({},{
				inputTxtId:$(this).attr('id')
			},$.selectDate.defaults,options);
			var config={
				mouseOnPanel:false,
				pageIndex:0
			};
			var $ele=$(this).closest('.'+options.divClass);
			var indexArr = options.IsYear?init.call($ele):initM.call($ele);
			$(this).click(function(){
				//options.IsYear?show.call(this):showM.call(this);
				if( $(this).siblings('#'+options.inputTxtId+'_div').css("visibility") == "hidden" ){
					options.IsYear?show.call(this):showM.call(this);
				}else{
					options.IsYear?hideResult():hideResultM();
				};
			}).blur(function(){
				//if(!config.mouseOnPanel)
				//	hideResult();
			});
			function getIndex(input){
				var curValue=parseInt(input.attr('page'));
				curValue=curValue || (new Date()).getFullYear();
				var mindex=0;					
				for(var m in indexArr){
					if(indexArr[m].max > curValue && indexArr[m].min <= curValue){
						mindex=m;break;								
					}
				}
				return mindex;
				
			}
			function show(){
				var input=$('#'+options.inputTxtId);
				var dval = parseInt(input.val());
				if( input.val() != "" ){
					if(input.val() == "至今" ){
						input.attr("page",(new Date()).getFullYear());
					}else{
						input.attr("page",dval);
					};
				};
				if(input.val()){
					if( isNaN(dval)){
						dval = 'sofar';
					};
				};
				
				$("#"+options.yInputId+"_FrameContainer").find("td").removeClass("selected");
				$('#'+options.yInputId+'_'+dval).addClass('selected');
				var index=getIndex(input);
				config.pageIndex=index;
				if(index == indexArr.length-1){
					$('#'+options.inputTxtId+'_next').hide();
					$('#'+options.inputTxtId+'_previous').show();
				}else if( index == 0 ){
					$('#'+options.inputTxtId+'_next').show();
					$('#'+options.inputTxtId+'_previous').hide();		
				}else{
					$('#'+options.inputTxtId+'_next').show();
					$('#'+options.inputTxtId+'_previous').show();
				};
				var divContainer=$('#'+options.inputTxtId+'_div');
				var frameContainer=$('#'+options.inputTxtId+'_FrameContainer');
				var div=$('#'+options.inputTxtId+'_frame'+index);
				div.css('width',options.showHeight+'px').siblings('div').css('width',options.height+'px');
				var left=0-(options.height*index);
				frameContainer.css('left',left+'px');
				setTitle(index);
				divContainer.css('visibility','visible');
				divContainer.siblings("i").removeClass("backDown").addClass("backUp").siblings("input").addClass("focus_color");
				$(document).bind("click", popClick);
				
			}
			function setTitle(index){
				var olm = indexArr[index];
				if( options.languageId == 1 ){
					$('#'+options.inputTxtId+'_div').find('.title em').html(olm.min+'年-'+(olm.max-1)+'年');
				}else{
					$('#'+options.inputTxtId+'_div').find('.title em').html(olm.min+'-'+(olm.max-1));
				};
			}
			function hideResult(){
				var divContainer=$('#'+options.inputTxtId+'_div');
				divContainer.css('visibility','hidden');
				divContainer.siblings("i").removeClass("backUp").addClass("backDown").siblings("input").removeClass("focus_color");
				optsFn(options.close,"closeYear");
				$(document).unbind("click", popClick);
			};

			function showM(){
				var input=$('#'+options.inputTxtId);
				var dval = parseInt(input.val());
				for( var i = 1 ; i < 13; i++ ){
					$('#'+options.mInputId+i).removeClass('selected');
				};
				$('#'+options.mInputId+dval).addClass('selected');
				var divContainer=$('#'+options.inputTxtId+'_div');
				divContainer.css('visibility','visible');
				divContainer.siblings("i").removeClass("backDown").addClass("backUp").siblings("input").addClass("focus_color");
				$(document).bind("click", popClickM);
			}
			function hideResultM(){
				var divContainer=$('#'+options.inputTxtId+'_div');
				divContainer.css('visibility','hidden');
				divContainer.siblings("i").removeClass("backUp").addClass("backDown").siblings("input").removeClass("focus_color");
				optsFn(options.closeM,"closeMonth");
				$(document).unbind("click", popClickM);
			}

			function initM(){
				inputR.parent().addClass(options.inputTxtId+"_this");
				var xy=$(this);
				var shtml='<div id="'+options.mInputId+'_div" class="zp_mr_dateDiv_m" style="left: '+options.left+'; top: '+xy.height()+'px; visibility: hidden;">';
				if(options.languageId == 1){
					shtml+='<div class="title">月份</div>';
				}else{
					shtml+='<div class="title">Month</div>';
				};
				shtml+='<div class="spacer clearfix">';
				shtml+='<div id="'+options.mInputId+'_content" class="content">';
				shtml+='<div style="" class="dataContainer" id="'+options.mInputId+'_dataContainer">';
				shtml+='<table cellspacing="1" cellpadding="0" border="0"><tbody>';
				var seed= 0,thtml='';
				for(var i=0;i<12;i++){
					if(i==0){
						thtml+='<tr>';
					}
					var p=i+1;
					if( options.languageId == 1 ){
						thtml+='<td width="20%" id="'+options.mInputId+''+p+'">'+p+'月</td>';
					}else{
						thtml+='<td width="20%" id="'+options.mInputId+''+p+'">'+p+'</td>';
					}
					if((seed+1)%5==0&&i<11){
						thtml+='</tr><tr><td class="line" colspan="5"><img height="1" width="1" src="http://my.zhaopin.com/images/blank.gif"></td></tr><tr>';
					}
					if(i==11){
						thtml+='</tr>';
					}
					seed++;
				}
				shtml+=thtml;
				shtml+='</tbody></table>';
				shtml+='</div>';
				shtml+='</div>';
				shtml+='</div>';
				shtml+='<div class="bottom"></div>';
				shtml+='</div>';
				this.append(shtml);
				$('#'+options.inputTxtId+'_dataContainer').find('table tr td').click(function(){
					inputR.val($(this).text());
					hideResultM();
				}).hover(function(){
					$(this).addClass('focus');
				},function(){
					$(this).removeClass('focus');
				});
				return [];


			}

			function init(){
				inputR.parent().addClass(options.inputTxtId+"_this");
				var data = inputR.attr("data");
				var dataArr = data.split('|');
				var dataLen = dataArr.length;
				options.itemLength = dataLen;
				var curYear= parseInt(dataArr[dataLen-1]);				
				curYear=curYear+1;
				if( options.soFar == true ){
					var pagecount=Math.ceil((options.itemLength+1)/15);
				}else{
					var pagecount=Math.ceil(options.itemLength/15);
				};
				var indexArr=[];
				for(var i=0;i<pagecount;i++){
					if( options.soFar == true ){
						if( i == 0){
							var max=curYear;
							var min=max-14;
						}else{
							var max=curYear-14-(15*(i-1));
							var min=max-15;
						};
					}else{;
						var max=curYear-(15*i);
						var min=max-15;
					};
					if(min<curYear-options.itemLength){
						min=curYear-options.itemLength;
					};
					indexArr.push({min:min,max:max});
				}
				indexArr.reverse();
				var xy=$(this);		
				var allheight=options.height*(pagecount-1)+options.showHeight;
				var shtml='<div id="'+options.yInputId+'_div" class="zp_mr_dateDiv_y" style="left: '+options.left+'; top: '+xy.height()+'px; visibility: hidden;">';
				shtml+='<div class="title"><img id="'+options.yInputId+'_previous" class="previous" title="更早的年份" src="http://img01.zpin.net.cn/2014/c/img/creat_resume/icon_y_control.gif" alt="prev" align="absmiddle"><em>1995-2005</em><img id="'+options.yInputId+'_next" class="next" title="更晚的年份" src="http://img01.zpin.net.cn/2014/c/img/creat_resume/icon_y_controlr.gif" align="absmiddle" alt="next"></div>';
				shtml+='<div class="spacer clearfix">';
				shtml+='<div id="'+options.yInputId+'_content" style="height:102px" class="content">';
				shtml+='<div style="width: '+allheight+'px; height: 102px; top: 0px; left: -888px;" class="frameContainer" id="'+options.yInputId+'_FrameContainer">';
				var thtml='';
				for(var i in indexArr){
					var minIndex=indexArr[i].min;
					var maxIndex=indexArr[i].max;
					var thtml='<div style="width: '+options.height+'px; height: 102px;" class="frame" id="'+options.yInputId+'_frame'+i+'"><table cellspacing="1" cellpadding="0" border="0"><tbody>';
					var seed=0;
					for(var n =minIndex;n<maxIndex;n++){
						if(n===minIndex){
							thtml+='<tr>';
						}
						if( options.languageId == 1 ){
							thtml+='<td  id="'+options.yInputId+'_'+n+'">'+n+'年</td>';
							if( options.soFar == true ){
								if( n == curYear - 1){
									thtml+='<td  id="'+options.yInputId+'_sofar">至今</td>';
								};
							};
						}else{
							thtml+='<td  id="'+options.yInputId+'_'+n+'">'+n+'</td>';
							if( options.soFar == true ){
								if( n == curYear - 1){
									thtml+='<td  id="'+options.yInputId+'_sofar">Now</td>';
								};
							};
						}
						
						if((seed+1)%5==0&&n<maxIndex-1){
							thtml+='</tr><tr><td class="line" colspan="5"><img height="1" width="1" src="http://my.zhaopin.com/images/blank.gif"></td></tr><tr>';
						}
						if(n===maxIndex-1){
							thtml+='</tr>';
						}
						seed++;
					}					
					thtml+='</tbody></table></div>';
					shtml+=thtml;
				}				
				shtml+='</div>';
				shtml+='</div>';
				shtml+='</div>';
				shtml+='<div class="bottom"></div>';
				shtml+='</div>';
				this.append(shtml);
				
				$('#'+options.inputTxtId+'_previous').click(function(){					
//					if(config.pageIndex < 1){
//						$(this).hide()
//						return;
//					}					
					pager(--config.pageIndex);
					if(config.pageIndex < 1){
						$(this).hide();
					}else{
						$('#'+options.inputTxtId+'_next').show();
					};
					
				});
				$('#'+options.inputTxtId+'_next').click(function(){					
//					if(config.pageIndex==indexArr.length-1){
//						alert('最后一页了');
//						return;
//					}
					pager(++config.pageIndex);
					if(config.pageIndex==indexArr.length-1){
						$(this).hide();
					}else{
						$('#'+options.inputTxtId+'_previous').show()
					};
					
				});
				$('#'+options.inputTxtId+'_FrameContainer').find('table tr td').click(function(){
					inputR.val($(this).text());
					hideResult();
					if( options.soFar == true ){
						if( $(this).attr("id") == options.inputTxtId+'_sofar' ){
							$('#'+options.mInputId).val("");
							$('#'+options.mInputId).parent().hide();
						}else{
							$('#'+options.mInputId).parent().show();
						};
					};
					if(	options.linkage == true && $(this).attr("id") != options.inputTxtId+'_sofar' ){
						var dval = parseInt($('#'+options.mInputId).val());
						for( var i = 1 ; i < 13; i++ ){
							$('#'+options.mInputId+i).removeClass('selected');
						};
						$('#'+options.mInputId+dval).addClass('selected');
						$('#'+options.mInputId+'_div').css('visibility','visible');
						$('#'+options.mInputId+'_div').siblings("i").removeClass("backDown").addClass("backUp").siblings("input").addClass("focus_color");
						$(document).bind("click",popClickMT);
					};
				}).hover(function(){
					$(this).addClass('focus');
				},function(){
					$(this).removeClass('focus');
				});
				$('#'+options.inputTxtId+'_div').mousedown(function () {
                	config.mouseOnPanel = true;
            	}).mouseup(function () {
                	config.mouseOnPanel = false;
            	});
				return indexArr;
			}
			function pager(index){
				setTitle(index);
				var frameContainer=$('#'+options.inputTxtId+'_FrameContainer');
				var left=0-(options.height*index);
				frameContainer.animate({left:left+'px'},200);
			}
			function isFn(obj){
				return !!(obj && obj.constructor && obj.call && obj.apply);
			};
			function optsFn(fn,fnName){
				if( isFn(fn) ){
					fn();
				}else{
					if(fn != null){
						alert(fnName + " is not a function");
					};
				};
			};
			function popClick(e){
				if ($(e.target).closest('.'+options.inputTxtId+"_this").length || !$(e.target).closest("body").length) {
					return;
				}
				hideResult();
			}
			function popClickM(e){
				if ($(e.target).closest('.'+options.inputTxtId+"_this").length || !$(e.target).closest("body").length) {
					return;
				}
				hideResultM();
			}
			function popClickMT(e){
				if ($(e.target).closest('.'+options.mInputId+"_this").length ||$(e.target).closest('#'+options.inputTxtId+'_FrameContainer').length || !$(e.target).closest("body").length) {
					return;
				};
				$('#'+options.mInputId+'_div').css('visibility','hidden');
				$('#'+options.mInputId+'_div').siblings("i").removeClass("backUp").addClass("backDown").siblings("input").removeClass("focus_color");
				optsFn(options.closeM,"closeMonth");
				$(document).unbind("click", popClickMT);
				
			}
		}	
		
	});

}).call(window,jQuery);